
package co.yixiang.modules.auction.service.impl;

import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.auction.domain.YxAuctionTakeDelivery;
import co.yixiang.modules.auction.service.YxAuctionTakeDeliveryService;
import co.yixiang.modules.auction.service.dto.YxAuctionTakeDeliveryDto;
import co.yixiang.modules.auction.service.dto.YxAuctionTakeDeliveryQueryCriteria;
import co.yixiang.modules.auction.service.mapper.YxAuctionTakeDeliveryMapper;
import co.yixiang.utils.FileUtil;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
* @author hupeng
* @date 2021-05-19
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "yxAuctionTakeDelivery")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxAuctionTakeDeliveryServiceImpl extends BaseServiceImpl<YxAuctionTakeDeliveryMapper, YxAuctionTakeDelivery> implements YxAuctionTakeDeliveryService {

    private final IGenerator generator;

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(YxAuctionTakeDeliveryQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YxAuctionTakeDeliveryDto> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", page.getList());
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<YxAuctionTakeDeliveryDto> queryAll(YxAuctionTakeDeliveryQueryCriteria criteria){
//        return baseMapper.selectList(QueryHelpPlus.getPredicate(YxAuctionTakeDelivery.class, criteria));
        return baseMapper.selectTakeList(criteria);
    }


    @Override
    public void download(List<YxAuctionTakeDeliveryDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxAuctionTakeDeliveryDto yxAuctionTakeDelivery : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("拍卖订单id", yxAuctionTakeDelivery.getOid());
            map.put("补价金额", yxAuctionTakeDelivery.getPrice());
            map.put("支付凭证", yxAuctionTakeDelivery.getPayImg());
            map.put("1 拍币  2 支付凭证", yxAuctionTakeDelivery.getPayType());
            map.put("0 - 需补价格 1 - 已补价|无需补价  2 - 已完成", yxAuctionTakeDelivery.getStatus());
            map.put("创建时间", yxAuctionTakeDelivery.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
